package service001;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    RedisTemplate<String, Integer> redisTemplate;


    @GetMapping("/insertOrder")
    public Object insertOrder() throws InterruptedException {
        synchronized (this){
            // 查询库存量
            Integer stock = redisTemplate.opsForValue().get("stock");
            if (stock > 0) {
                // 如果库存 > 0， 库存 -1
                Integer num = stock - 1;
                redisTemplate.opsForValue().set("stock", num);
                System.out.println("下单成功，当前剩余库存量：" + num);
            } else {
                System.out.println("下单失败，库存已被秒空");
            }
            return stock;
        }
    }

}
